package com.eurake.seckill.rabbitmq;

import com.eurake.seckill.pojo.SeckillOrders;
import com.eurake.seckill.service.IGoodsService;
import com.eurake.seckill.service.IOrderService;
import com.eurake.seckill.utils.JSONUtil;
import com.eurake.seckill.vo.GoodsVo;
import com.eurake.seckill.vo.RespBean;
import com.eurake.seckill.vo.RespBeanEnum;
import com.eurake.seckill.vo.SeckillMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import com.eurake.seckill.pojo.User;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQReceiver {
//    @RabbitListener(queues = "queue")
//    public void receive(Object message) {
//        log.info("receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue01")
//    public void receive01(Object message) {
//        log.info("receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue02")
//    public void receive02(Object message) {
//        log.info("receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue_direct01")
//    public void receiveDirect01(Object message) {
//        log.info("queue_direct01 receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue_direct02")
//    public void receiveDirect02(Object message) {
//        log.info("queue_direct02 receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue_topic01")
//    public void receiveTopic01(Object message) {
//        log.info("queue_topic01 receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue_topic02")
//    public void receiveTopic02(Object message) {
//        log.info("queue_topic02 receive message:{}", message);
//    }
//
//    @RabbitListener(queues = "queue_headers01")
//    public void receiveHeaders01(Message message) {
//        log.info("queue_headers01 receive message:{}", message);
//        log.info("queue_headers01 receive message:{}", new String(message.getBody()));
//    }
//
//    @RabbitListener(queues = "queue_headers02")
//    public void receiveHeaders02(Message message) {
//        log.info("queue_headers02 receive message:{}", message);
//        log.info("queue_headers02 receive message:{}", new String(message.getBody()));
//    }

    @Resource
    IGoodsService goodsService;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    IOrderService orderService;

    @RabbitListener(queues = "seckillQueue")
    public void receive(String message) {
        log.info("receive message:{}", message);
        SeckillMessage seckillMessage = JSONUtil.jsonStr2Object(message, SeckillMessage.class);
        Long goodsId = seckillMessage.getGoodsId();
        User user = seckillMessage.getUser();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        if(goodsVo.getStockCount() <= 0) {
            return ;
        }
        //判断是否重复抢购
        SeckillOrders seckillOrders = (SeckillOrders)redisTemplate.opsForValue().get("order:"+user.getId()+":"+goodsId);
        if(seckillOrders!=null){
            return;
        }

        //下单操作
        orderService.seckill(user,goodsVo);
        return ;
    }
}
